-- log_mgr.lua
-- 日志管理器

declare_module('LOG_MGR')
local self = LOG_MGR;

require('logging')
require('logging.console')
require('logging.file')
require('logging.sql')

local m_log_file;

-- log level
-- if log level is 0, not output info
-- if log level is 1, output info
_LOG_LEVEL = 1

if _LOG_TYPE == nil then
    -- 自定义的默认log
    _G.logger = logging.new(  
        function(self, level, ...)
            local message = string.format(...);
            if level == 'ERROR' then
                local debug_info = debug.getinfo(4);
                if debug_info ~= nil then
                    local logmsg = string.format("%s %s %s\n%s:%d",
                        os.date("%H:%M:%S "),
                        level,
                        tostring(message),
                        debug_info.source,
                        debug_info.currentline);
                    colorPrint(logmsg, PrintColor.IRED);
                    log(logmsg);
                else
                    local msg = os.date("%H:%M:%S ")..level..' '..tostring(message);
                    colorPrint(msg, PrintColor.IRED);
                    log(msg);
                end
                colorPrint(debug.traceback(), PrintColor.IRED);
                log(debug.traceback());
                flush();
            elseif level == 'WARN' then
                local msg = os.date("%H:%M:%S ")..level..' '..tostring(message);
                colorPrint(msg, PrintColor.IYELLOW);
                log(os.date("%H:%M:%S ")..level..' '..tostring(message));
            elseif level == 'INFO' then
                local msg = os.date("%H:%M:%S ")..level..' '..tostring(message);
                print(msg);
                log(msg);
            elseif level == 'FATAL' then
                local msg = os.date("%H:%M:%S ")..level..' '..tostring(message);
                colorPrint(msg, PrintColor.IRED);
                log(msg);
                flush();
            end
            return true
        end
    );

	_G.logger.log = function (self, level, ...)
        message = string.format(...);
		if type(message) ~= "string" then
		  message = tostring(message)
		end
		return logger:append(level, ...)
	end

    _G.logger.debug = function (logger, ...) return logger:log(logging.DEBUG, ...) end
	_G.logger.info  = function (logger, ...) return logger:log(logging.INFO,  ...) end
	_G.logger.warn  = function (logger, ...) return logger:log(logging.WARN,  ...) end
	_G.logger.error = function (logger, ...) return logger:log(logging.ERROR, ...) end
	_G.logger.fatal = function (logger, ...) return logger:log(logging.FATAL, ...) end

    -- 如果level为0，不打印普通LOG
    if _LOG_LEVEL == 0 then
        _G.logger.info = function() end
    end

elseif _LOG_TYPE == 'console' then
    _G.logger = logging.console();
elseif _LOG_TYPE == 'file' then
    _G.logger = logging.file();
elseif _LOG_TYPE == 'sql' then
    _G.logger = logging.sql();
end

-- 设置日志文件
function set_log_file(log_file)
    if log_file then
        m_log_file = io.open(log_file, 'w');
    else
        m_log_file = nil;
    end
end

-- 记录到日志文件
function log(...)
    if m_log_file then
        m_log_file:write(...);
        m_log_file:write('\n');
    end
end

function flush()
    if m_log_file then
        m_log_file:flush();
    end
end

